package com.yuntsg.nnsfcp.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import com.yuntsg.nnsfcp.common.utils.CheckUtil;
import com.yuntsg.nnsfcp.common.utils.RedisUtil;
import com.yuntsg.nnsfcp.common.utils.StringUtil;
import com.yuntsg.nnsfcp.common.utils.UserUtil;
import com.yuntsg.nnsfcp.entity.BiaoshuEntity;
import com.yuntsg.nnsfcp.front.entity.BidDocumentArticle;
import com.yuntsg.nnsfcp.front.entity.BidDocumentRecordArticle;
import com.yuntsg.nnsfcp.front.mapper.BidBackCompareMapper;
import com.yuntsg.nnsfcp.front.mapper.BidDocumentArticleMapper;
import com.yuntsg.nnsfcp.front.mapper.BidDocumentRecordArticleMapper;
import com.yuntsg.nnsfcp.service.CommonService;
import com.yuntsg.nnsfcp.service.back.BackService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/front")
//@Api(tags = "前端控制器", value = "前端控制器", description = "前端控制器")
public class FrontController {

    @Resource
    BidBackCompareMapper bidBackCompareMapper;

    @Resource
    BidDocumentRecordArticleMapper bidDocumentRecordArticleMapper;

    @Resource
    BidDocumentArticleMapper bidDocumentArticleMapper;

    @Resource
    UserUtil userUtil;

    @Resource
    CommonService commonService;

    @Resource
    BackService backService;

    @SneakyThrows
    @PostMapping("add-pdf-file")
//    @ApiOperation(value = "1上传pdf 进行解析返回数据uuid 下一步根据uuid 进行获取铺上数据")
    public ResultBean<Object> addPdfFile(MultipartFile file) {
        BiaoshuEntity biaoshuEntity = backService.parsePdfToEntity(file, null, null);
        if (biaoshuEntity == null) {
            CheckUtil.check(false, "pdf 解析异常");
        }
        // 返回给前端数据 进行根据id 进行保存返回
        String string = UUID.randomUUID().toString();
        RedisUtil.saveObject(string, biaoshuEntity, 18000);
        return new ResultBean<>(string);
    }

    @GetMapping("get-pdf-parse-res")
//    @ApiOperation(value = "1.1获取pdf数据进行显示")
    public ResultBean<Object> getPdfParseRes(String uuid) {
        CheckUtil.check(StringUtil.isNotNull(uuid), " uuid is not null");
        Object string = RedisUtil.getObject(uuid);
        if (string != null) {
            return new ResultBean<>(string);
        } else {
            CheckUtil.check(false, "非法uuid 或者解析错误");
        }
        return new ResultBean<>(null);

    }


    @PostMapping("/up-info-check")
//    @ApiOperation(value = "2提取信息核对")
    public ResultBean<Object> upInfoCheck(@RequestBody BiaoshuEntity biaoshuEntity) {
        // 标记错误信息
        CheckUtil.check(StringUtil.isNotNull(biaoshuEntity), "请传递实体类++");
        backService.addModifyBidInfo(biaoshuEntity);
        return new ResultBean<>();
    }

    @GetMapping("get-bid-art-list")
    public ResultBean<Object> getBidArtList(String bidid) {
        CheckUtil.check(StringUtil.isNotNull(bidid), "bidid 不能空");
        Object string = RedisUtil.getObject(bidid);
        if (string != null) {
            BiaoshuEntity biaoshuEntity = (BiaoshuEntity) string;
            List<BidDocumentArticle> bdId = bidDocumentArticleMapper.selectList(new QueryWrapper<BidDocumentArticle>().eq("bd_id", biaoshuEntity.getBdraid()));
            return new ResultBean<>(bdId);
        } else {
            CheckUtil.check(false, "非法uuid 或者解析错误");
        }
        return new ResultBean<>(null);
    }

    @PostMapping("/up-art-doi")
//    @ApiOperation(value = "3更新文章信息")
    public ResultBean<Object> upInfoCheck(@RequestBody BidDocumentArticle[] bidDocumentArticles) {
        // 标记错误信息
        for (BidDocumentArticle bidDocumentArticle : bidDocumentArticles) {
            CheckUtil.check(StringUtil.isNotNull(bidDocumentArticle.getId()), "实体类id不能空");
            bidDocumentArticleMapper.updateById(bidDocumentArticle);
        }
        return new ResultBean<>();
    }

    @GetMapping("commit-bid")
//3
    public ResultBean<Object> commitbid(@RequestParam(value = "bidid") String bidid) {
        CheckUtil.check(StringUtil.isNotNull(bidid), "bidid 不能空");

        Object string = RedisUtil.getObject(bidid);
        if (string != null) {
            BiaoshuEntity biaoshuEntity = (BiaoshuEntity) string;
            BidDocumentRecordArticle bidDocumentRecordArticle = bidDocumentRecordArticleMapper.selectById(biaoshuEntity.getBdraid());
            CheckUtil.check(StringUtil.isNotNull(bidDocumentRecordArticle), "非法访问 不能空");
            bidDocumentRecordArticle.setStat(3);
            bidDocumentRecordArticle.setUpti(LocalDateTime.now());
            bidDocumentRecordArticleMapper.updateById(bidDocumentRecordArticle);
            commonService.addWosRe(bidDocumentRecordArticle);
            return new ResultBean<>("");

        } else {
            CheckUtil.check(false, "非法访问");
        }
        return new ResultBean<>();

    }

    @GetMapping("/get-user-info")
    public ResultBean<Object> getUserInfo() {
        String uname = userUtil.getFrontUname();
        return new ResultBean<>(uname);
    }

    @GetMapping("/get-commit-list")
//    @ApiOperation(value = " 5 获取用户提交的历史记录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码"),
//            @ApiImplicitParam(name = "limit", value = "数量")
//    })
    public ResultBean<Object> getCommitList(Integer page, Integer limit) {
        Integer frontUid = userUtil.getFrontUid();
        IPage iPage = new Page(page, limit);
        IPage iPage1 = bidDocumentRecordArticleMapper.selectPage(iPage, new QueryWrapper<BidDocumentRecordArticle>().eq("uid", frontUid).ne("stat", 1).orderByDesc("id"));
        return new ResultBean<>(iPage1);

    }

    @GetMapping("/get-commit-list-detail")
//    @ApiOperation(value = " 5.0 获取单个详情页")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "主键id"),
//
//    })
    public ResultBean<Object> getCommitListDetail(Integer id) {
        Integer frontUid = userUtil.getFrontUid();
        BidDocumentRecordArticle bidDocumentRecordArticle = bidDocumentRecordArticleMapper.selectById(id);
        if (bidDocumentRecordArticle.getUid().intValue() != frontUid.intValue()) {
            CheckUtil.check(false, "非法访问");
        }
        return new ResultBean<>(bidDocumentRecordArticle);

    }

    @GetMapping("get-bid-art-list-his")
//    @ApiOperation(value = "5.1获取历史记录对应文献信息列表页的数据")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "标书id"),
//            @ApiImplicitParam(name = "page", value = "页码"),
//            @ApiImplicitParam(name = "limit", value = "数量"),
//    })
    public ResultBean<Object> getBidArtListHis(Integer id, Integer page, Integer limit) {
        CheckUtil.check(StringUtil.isNotNull(id), "id 不能空");
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        IPage iPage = new Page(page, limit);
        BidDocumentRecordArticle bidDocumentRecordArticle = bidDocumentRecordArticleMapper.selectById(id);
        QueryWrapper<BidDocumentArticle> bdId1 = new QueryWrapper<BidDocumentArticle>().eq("bd_id", bidDocumentRecordArticle.getId());
        CheckUtil.check(StringUtil.isNotNull(bidDocumentRecordArticle), "非法访问");
        IPage iPage1 = bidDocumentArticleMapper.selectPage(iPage, bdId1);
        return new ResultBean<>(iPage1);
    }


    @PostMapping("/tip-error-msg")
//    @ApiOperation(value = "标记错误信息--此方法作废")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "bidid", value = "标书id"),
//            @ApiImplicitParam(name = "type", value = "类型"),
//            @ApiImplicitParam(name = "error", value = "错误信息")
//    })
    public ResultBean<Object> tipErrorMsg(Integer bidid, String type, String error) {
        // 标记错误信息
        CheckUtil.check(StringUtil.isNotNull(bidid), "bidid is null");
        CheckUtil.check(StringUtil.isNotNull(type), "type is null");
        CheckUtil.check(StringUtil.isNotNull(error), "error is null");
        backService.addBidErrorMsg(bidid, type, error);
        return new ResultBean<>();
    }


//    @GetMapping("/get-nnsfcp-list")
//    @ApiOperation(value = "获取基金信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "year", value = "年份"),
//            @ApiImplicitParam(name = "class", value = "机构"),
//            @ApiImplicitParam(name = "dep", value = "部门"),
//            @ApiImplicitParam(name = "limit", value = "数量"),
//            @ApiImplicitParam(name = "page", value = "页码")
//    })
//    public ResultBean<Object> getNNsfcpList(Integer year, String className, String dep, Integer limit, Integer page) {
//        if (StringUtil.isNull(page) || StringUtil.isNull(limit)) {
//            page = 1;
//            limit = 10;
//
//        }
//        IPage iPage2 = new Page(page, limit);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if (StringUtil.isNotNull(year)) {
//            queryWrapper.ge("year", year);
//        }
//        if (StringUtil.isNotNull(className)) {
//            queryWrapper.like("class", className);
//        }
//        if (StringUtil.isNotNull(dep)) {
//            queryWrapper.like("dep", dep);
//        }
//        IPage iPage = fundTabMapper.selectMapsPage(iPage2, queryWrapper);
//        return new ResultBean<>(iPage);
//
//    }
}
